//
//  GYHomeShopDetailVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/10.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYHomeShopDetailVC.h"
#import "DWSegmentedControl.h"
#import "Head.h"
#import "GYShopInfoViewModel.h"
#import "GYShopinfoView.h"
#import "GYShopProductListView.h"
#import "GYHomeSaleDetailVC.h"
@interface GYHomeShopDetailVC ()<DWSegmentedControlDelegate>
{
    NSInteger currentPage;
}

@property(nonatomic,strong)UIScrollView* mainScrollView;
@property(nonatomic,strong)UIView* titleView;
@property(nonatomic,strong)GYShopinfoView* ShopinfoView;
@property(nonatomic,strong)GYShopProductListView* ShopListView;
@property(nonatomic,strong)GYChooseView* chooseView;
@property(nonatomic,strong)GYShopInfoViewModel* shopInfoModel;
@property(nonatomic,strong)NSMutableDictionary* paraDataSource;
@end

@implementation GYHomeShopDetailVC
#pragma mark - getter and setter
-(NSMutableDictionary *)paraDataSource{
    
    if (!_paraDataSource) {
        _paraDataSource=[[NSMutableDictionary alloc] init];
    }
    return _paraDataSource;
}
-(GYChooseView *)chooseView
{
    if (!_chooseView) {
        _chooseView=[[GYChooseView alloc] initWithFrame:CGRectMake(self.view.width, 0, self.view.width, WFCGFloatY(45)) chooseViewType:ChooseViewTypeSingleCenter];
        _chooseView.backgroundColor=[UIColor whiteColor];
        
        [WFFunctions WFUIaddBorderToView:_chooseView top:NO left:0 bottom:1 right:0 borderColor:RGBACOLOR(240, 240, 240, 1) borderWidth:0.5];
        @WeakSelf;
        _chooseView.chooseBack = ^(NSDictionary *paraDic) {
            for (NSString* key in paraDic.allKeys)
            {
                [selfp.paraDataSource setObject:paraDic[key] forKey:key];
            }
            currentPage=1;
            [selfp.paraDataSource setObject:@"1" forKey:@"page"];
            [selfp loadListData];
        };
        _chooseView.categoryback = ^(NSString *categoryStr) {
            currentPage=1;
            [selfp.paraDataSource setObject:@"1" forKey:@"page"];
            [selfp.paraDataSource setObject:categoryStr forKey:@"sort"];
            [selfp loadListData];
            
        };
    }
    return _chooseView;
}

-(GYShopinfoView *)ShopinfoView
{
    if (!_ShopinfoView) {
        _ShopinfoView=[[GYShopinfoView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, _mainScrollView.height)];
        @WeakSelf;
        _ShopinfoView.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [selfp loadData];
        }];
    }
    return _ShopinfoView;
}
-(GYShopProductListView *)ShopListView
{
    if (!_ShopListView) {
        _ShopListView=[[GYShopProductListView alloc] initWithFrame:CGRectMake(self.view.width, WFCGFloatY(45), self.view.width, _mainScrollView.height-WFCGFloatY(45))];
        @WeakSelf;
        _ShopListView.callBack = ^(NSString *proid) {
            GYHomeSaleDetailVC* gy=[[GYHomeSaleDetailVC alloc] init];
            gy.passID=proid;
            [selfp.navigationController pushViewController:gy animated:YES];
        };
        
        _ShopListView.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            currentPage = 1;
            [selfp setOriginData];
            [selfp loadListData];
        }];
        _ShopListView.myTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            if (selfp.shopInfoModel.canAddMore)
            {
                currentPage += 1;
                [selfp.paraDataSource setObject:[NSString stringWithFormat:@"%ld",currentPage] forKey:@"page"];
                [selfp loadListData];
            }else
            {
                [_ShopListView.myTable.mj_footer endRefreshingWithNoMoreData];
            }
            
        }];
    }
    return _ShopListView;
}
-(GYShopInfoViewModel *)shopInfoModel
{
    if (!_shopInfoModel) {
        _shopInfoModel=[[GYShopInfoViewModel alloc] init];
    }
    return _shopInfoModel;
}
-(UIScrollView *)mainScrollView{
    
    if (!_mainScrollView) {
        _mainScrollView=[[UIScrollView alloc] init];
        _mainScrollView.frame=CGRectMake(0, 0, self.view.width, self.view.height-GY_Nav_height);
        _mainScrollView.backgroundColor=[UIColor whiteColor];
        _mainScrollView.contentSize=CGSizeMake(self.view.width*2, _mainScrollView.height);
        _mainScrollView.pagingEnabled=YES;
        _mainScrollView.delegate=self;
        _mainScrollView.showsVerticalScrollIndicator=NO;
        _mainScrollView.showsHorizontalScrollIndicator=NO;
    }
    return _mainScrollView;
    
}

-(UIView *)titleView
{
    if (!_titleView)
    {
        _titleView=[[UIView alloc] init];
        _titleView.frame=CGRectMake(0, 0, 200, 44);
        _titleView.backgroundColor=[UIColor clearColor];
        for (int i=0; i<2; i++)
        {
            UIButton* btn=[UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame=CGRectMake(200/2*i, 4, 200/2, 40);
            [btn setTitle:i==0?@"企业信息":@"产品" forState:UIControlStateNormal];
            [_titleView addSubview:btn];
            btn.tag=400+i;
            [btn setTitleColor:RGBACOLOR(255, 255, 255, 0.7) forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            btn.titleLabel.font=SYSTEMFONT(15);
            [btn addTarget:self action:@selector(pressindex:) forControlEvents:UIControlEventTouchUpInside];
            if (i==0) {
                btn.selected=YES;
            }
        }
        
        UIImageView* line=[[UIImageView alloc] init];
        line.frame=CGRectMake(0, 42, 200/2, 1.5);
        line.backgroundColor=[UIColor whiteColor];
        line.tag=300;
        [_titleView addSubview:line];
    }
    return _titleView;
}

#pragma mark - event
#pragma mark - method

-(void)setOriginData
{
    currentPage = 1;
    for (NSString* key in self.paraDataSource.allKeys) {
        if (![key isEqualToString:@"sort"])
        {
            [self.paraDataSource removeObjectForKey:key];
        }
    }
    self.paraDataSource[@"userid"]=[NSString stringWithFormat:@"%@",_passID];
    self.paraDataSource[@"page"]=@"1";
    self.paraDataSource[@"pagesize"]=@"10";
}

-(void)pressindex:(UIButton* )sender
{
    [self lineScroll:sender.tag-400];
}
-(void)lineScroll:(NSInteger)index
{
    for (int i=0; i<2; i++)
    {
        UIButton* btn=(UIButton* )[_titleView viewWithTag:400+i];
        if (index==i)
        {
            btn.selected=YES;
        }else
        {
            btn.selected=NO;
        }
       
    }
    
    UIImageView* line=(UIImageView* )[_titleView viewWithTag:300];
    [UIView animateWithDuration:.5 animations:^{
        line.frame=CGRectMake((200/2)*index, 42, 200/2, 1.5);
    }];
    //indexCount=index;
    [_mainScrollView scrollRectToVisible:CGRectMake(self.view.width*index, 0, self.view.width, _mainScrollView.bounds.size.height) animated:NO];
}

-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel* saleOber=[GCBaseObservationModel new];
    saleOber.keyPath=@"infoModel";
    saleOber.observation=self.shopInfoModel;
    saleOber.handler = ^(NSString *keyPath) {

        [self.ShopinfoView refresh:self.shopInfoModel.infoModel];
    };
    [self registObservation:saleOber];

    GCBaseObservationModel* buyOber=[GCBaseObservationModel new];
    buyOber.keyPath=@"datas";
    buyOber.observation=self.shopInfoModel;
    buyOber.handler = ^(NSString *keyPath) {
        [selfp.ShopListView refresh:self.shopInfoModel];
    };
    [self registObservation:buyOber];
    
    
    GCBaseObservationModel *observation1 = [GCBaseObservationModel new];
    observation1.observation = self.shopInfoModel;
    observation1.keyPath = @"totalCount";
    observation1.handler = ^(NSString *keyPath) {
        [selfp.chooseView setTotalCount:selfp.shopInfoModel.totalCount];
    };
    [self registObservation:observation1];
}


-(void)loadData
{
    NSMutableDictionary* dic=[[NSMutableDictionary alloc] init];
    [dic setObject:_passID forKey:@"id"];
    [self.shopInfoModel requestInfo:dic];
}
-(void)loadListData
{
    [self.shopInfoModel requestDatas:self.paraDataSource];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title=@"求购信息";
    [self viewModelBand];

    self.navigationItem.titleView=self.titleView;
    [self.mainScrollView addSubview:self.ShopinfoView];
    [self.mainScrollView addSubview:self.ShopListView];
    [self.mainScrollView addSubview:self.chooseView];
    [self.chooseView setListFrame:self.view.width];
    [self.view addSubview:self.mainScrollView];
    
    [self loadData];
    [self setOriginData];
    [self loadListData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - delegate
-(void)dw_segmentedControl:(DWSegmentedControl *)control didSeletRow:(NSInteger)row
{
    NSLog(@"");
    [_mainScrollView scrollRectToVisible:CGRectMake(self.view.width*row, 0, self.view.width, _mainScrollView.height) animated:NO];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==_mainScrollView)
    {
        UIImageView* line=(UIImageView* )[_titleView viewWithTag:300];
         line.frame=CGRectMake(100*(scrollView.contentOffset.x/self.view.width), 42, 200/2, 1.5);
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView==_mainScrollView)
    {
        if (scrollView.contentOffset.x==self.view.width)
        {
            [self lineScroll:1];
        }
        if (scrollView.contentOffset.x==0)
        {
            [self lineScroll:0];
        }
    }
}
#pragma mark - delegate
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
